// src/scenarios/traffic_engineering.rs

//! 交通工程场景
//! 支持交通管理的语义转换和OPC UA模型构建

use crate::scenarios::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// 交通工程场景
pub struct TrafficEngineeringScenario {
    config: ScenarioConfig,
}

/// 交通信号状态
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TrafficSignalStatus {
    /// 红灯
    Red,
    /// 绿灯
    Green,
    /// 黄灯
    Yellow,
    /// 闪烁
    Flashing,
    /// 熄灭
    Off,
}

impl TrafficEngineeringScenario {
    pub fn new() -> Self {
        Self {
            config: ScenarioConfig {
                name: Some("交通工程语义转换场景".to_string()),
                description: Some("支持交通管理数据的语义转换和OPC UA模型构建，包括信号灯控制、路况监测、车流统计等".to_string()),
                custom_rules: None,
                extensions: HashMap::new(),
            },
        }
    }

    pub fn new_with_config(config: ScenarioConfig) -> Self {
        Self { config }
    }
}

impl Scenario for TrafficEngineeringScenario {
    fn name(&self) -> &'static str {
        "Traffic Engineering Semantic Conversion"
    }

    fn description(&self) -> &'static str {
        "交通工程语义转换场景，支持交通管理数据的语义转换"
    }

    fn industry(&self) -> &'static str {
        "TrafficEngineering"
    }

    fn get_transformation_rules(&self) -> ScenarioTransformationRules {
        ScenarioTransformationRules {
            rule_name: "Traffic_Engineering_General".to_string(),
            data_source_mappings: HashMap::new(),
            unit_conversions: HashMap::new(),
            data_type_mappings: HashMap::new(),
            semantic_mappings: HashMap::new(),
            constraints: vec![],
        }
    }

    fn get_opcua_model_template(&self) -> ScenarioModelTemplate {
        ScenarioModelTemplate {
            template_name: "TrafficEngineeringModel".to_string(),
            namespaces: vec![
                "http://opcfoundation.org/UA/".to_string(),
                "http://industryagent.com/traffic/".to_string(),
            ],
            object_types: vec![
                ObjectTypeTemplate {
                    type_name: "TrafficSignalType".to_string(),
                    description: Some("交通信号灯类型".to_string()),
                    parent_type: Some("BaseObjectType".to_string()),
                    properties: vec![
                        PropertyTemplate {
                            name: "SignalId".to_string(),
                            data_type: "String".to_string(),
                            description: Some("信号灯ID".to_string()),
                            unit: None,
                            required: true,
                            default_value: None,
                        },
                        PropertyTemplate {
                            name: "SignalStatus".to_string(),
                            data_type: "TrafficSignalStatusEnum".to_string(),
                            description: Some("信号状态".to_string()),
                            unit: None,
                            required: true,
                            default_value: Some(serde_json::json!("Red")),
                        },
                    ],
                    methods: vec![],
                },
            ],
            variable_types: vec![],
            reference_types: vec![],
        }
    }

    fn get_sample_data(&self) -> Vec<ScenarioSampleData> {
        vec![
            ScenarioSampleData {
                data_id: "traffic_light_001".to_string(),
                data_source_type: "HuaweiCloudIoT".to_string(),
                raw_data: serde_json::json!({
                    "signal_id": "TL-001",
                    "location": "十字路口-人民路与解放路",
                    "status": "Green",
                    "vehicle_count": 45,
                    "average_speed": 35.2,
                    "timestamp": "2024-01-15T10:30:00Z"
                }),
                expected_opcua: serde_json::json!({
                    "nodeId": "ns=5;i=1001",
                    "nodeClass": "Object",
                    "browseName": "TL-001"
                }),
                description: Some("交通信号灯数据示例".to_string()),
                category: Some("TrafficControl".to_string()),
            },
        ]
    }

    fn get_semantic_mappings(&self) -> HashMap<String, String> {
        let mut mappings = HashMap::new();
        mappings.insert("traffic_light".to_string(), "TrafficSignal".to_string());
        mappings.insert("road_sensor".to_string(), "TrafficSensor".to_string());
        mappings
    }
}